package com.net.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.net.model.Category;
import com.net.model.Commodity;
import com.net.service.CategoryService;
import com.opensymphony.xwork2.ActionSupport;

@Component("Category-*")
@Scope("prototype")
public class CategoryAction extends ActionSupport {
	private Category category;
	private CategoryService categoryService;
	private List<Category> categories;
	private String[] selectedList;
	private String cid;
	private String orderBy;
	private String keyword;
	private boolean success;

	public String add() throws Exception {
		// System.out.println(category.getCategory_name()+"llllll");
		if (selectedList != null) {
			for (int i = 0; i < selectedList.length; i++) {
				System.out.println(Integer.parseInt(selectedList[i]));
				if (categories.get(Integer.parseInt(selectedList[i])).getCategory_name().equals("")) {

				} else {
					this.categoryService.add(categories.get(Integer
							.parseInt(selectedList[i])));
				}
			}
		}
		this.list();
		return "addCategory";
	}

	public String delete() throws Exception {
		Set<Commodity> commodities = new HashSet<Commodity>();
		Set<Commodity> commodities1 = new HashSet<Commodity>();
		category = categoryService.loadCategoryById(category.getId());
		commodities = category.getCommodities();
		for(Commodity c : commodities){
			c.setProviders(null);
			commodities1.add(c);
		}
		category.setCommodities(commodities1);
		this.categoryService.delete(category);
		this.list();
		// System.out.println(category.getId()+":"+category.getcategory_name());
		return "deleteCategory";
	}

	public String deleteSelected() throws Exception {
		// System.out.println(selectedList.length+"  :8888888888888888");
		Category cust = new Category();
		if (selectedList != null) {
			for (int i = 0; i < selectedList.length; i++) {
				System.out.println(Integer.parseInt(selectedList[i]));
				cust.setId(Integer.parseInt(selectedList[i]));
				this.categoryService.delete(cust);
			}
		}
		this.list();
		return "deleteSelectedCategory";
	}

	public String list() throws Exception {
		this.categories = categoryService.getCategories();
		//System.out.println(categories.get(0).getCategory_name()+"++++++++++++++");
		success = true;
		return "listCategory";
	}

	public String load() {
		this.category = this.categoryService.loadCategoryById(category.getId());
		return "loadCategory";
	}

	public String update() {
		// System.out.println(category.getId()+"#################");
		category = categoryService.loadCategoryById(category.getId());
		// System.out.println(provider.getId()+"#################");
		return "updateCategory";
	}

	public String updateCommit() throws Exception {
		// System.out.println(category.getId()+"*****##########");
		categoryService.update(category);
		this.list();
		return "updateCategoryCommit";
	}

	public String search() {
		this.categories = categoryService.searchList(cid, keyword, orderBy);
		return "searchCategory";
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	@Resource(name = "categoryService")
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public String[] getSelectedList() {
		return selectedList;
	}

	public void setSelectedList(String[] selectedList) {
		this.selectedList = selectedList;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

}
